package structuralpattern.flyweightpattern;
/**
 * 邢帅教育
 * <p>
 * 本源代码由数据中心及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2018. xsteach.com all rights reserved.
 */

import structuralpattern.flyweightpattern.cup.Cup;
import structuralpattern.flyweightpattern.cup.TeaCup;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author maikec
 * @date 2018/9/10
 */
public class CupFactory {
    private static Map<String, Cup> cache = new ConcurrentHashMap<>( 16 );

    public static Cup getCup(String color) {
        if ( Objects.isNull( color ) ) {
            throw new NullPointerException( "color is must not null" );
        }
        if (Objects.isNull( cache.get( color ) )) {
            cache.put( color, new TeaCup( color ) );
        }
        return cache.get( color );
    }
}
